package sg.edu.nus.iss.billing.ui.gui;

import java.awt.*;

import javax.swing.*;

import sg.edu.nus.iss.billing.ui.controller.OneBillingSystem;
import sg.edu.nus.iss.billing.vo.subscription.*;
import sg.edu.nus.iss.billing.vo.subscription.feature.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.util.*;
import java.util.List;


public class MobileVoicePanel  extends JPanel {

	private static final long serialVersionUID = 1L;
	private TabbedFrame parent;
	private OneBillingSystem onebillingsystem;
	private MobileVoiceVO mobilevoiceVO;
	private MobileVoiceVO subscribedmobilevoiceVO;
	private ArrayList<FeatureVO> subscribedoptionalfeatures;
	
	JPanel selectionPanel;
	JPanel basicFeaturePanel;
	JPanel optionalFeaturePanel;
	JPanel buttonPanel;
	JCheckBox chkexclude;
	
	private boolean isParentVORequired = false;
	
	public MobileVoicePanel(TabbedFrame parent, OneBillingSystem onebillingsystem){
		this.parent= parent;
		this.onebillingsystem=onebillingsystem;
		Iterator<SubscriptionVO> iterator = parent.getAvailableplanoptions().iterator();
		SubscriptionVO plan;
		while (iterator.hasNext()) {
			plan= iterator.next();
			if( plan instanceof MobileVoiceVO){
				mobilevoiceVO = (MobileVoiceVO) plan;
				System.out.println(mobilevoiceVO.toString());
				System.out.println("---1---");
				System.out.println(mobilevoiceVO.getDescription());
				System.out.println("---2---");
				System.out.println(mobilevoiceVO.getMonthlySuscription());
				System.out.println("---3---");
				System.out.println(mobilevoiceVO.getAllOptionalFeature().toArray().length);
			}
		}

		Iterator<SubscriptionVO> iterator2 = parent.getSubscripedplanoptions().iterator();
		SubscriptionVO plan2;
		while (iterator2.hasNext()) {
			plan2= iterator2.next();
			if( plan2 instanceof MobileVoiceVO){
				subscribedmobilevoiceVO = (MobileVoiceVO) plan2;
			}
		}
		if(subscribedmobilevoiceVO==null){
			subscribedmobilevoiceVO = new MobileVoiceVO();
		}
		subscribedoptionalfeatures= subscribedmobilevoiceVO.getAllOptionalFeature();
		
		selectionPanel=createSelectionPanel();
		basicFeaturePanel=createBasicFeaturePanel();
		optionalFeaturePanel=createOptionalFeaturePanel();
		buttonPanel=createButtonPanel();
		
		 GroupLayout layout = new GroupLayout(this);
	        setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                //.addGap(24, 24, 24)
	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                    .addComponent(selectionPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                    .addComponent(basicFeaturePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                    .addComponent(optionalFeaturePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                    .addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
	                .addContainerGap(108, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	               // .addGap(29, 29, 29)
	                .addComponent(selectionPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                .addComponent(basicFeaturePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                .addComponent(optionalFeaturePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                .addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(214, Short.MAX_VALUE))
	        );
	}
	
	private void performchkexcludeChange(){
		if(!chkexclude.isSelected()){
        	basicFeaturePanel.setVisible(true);
        	optionalFeaturePanel.setVisible(true);
        	parent.setSubscripedplanoptions(parent.getTab().getSelectedIndex(),subscribedmobilevoiceVO);
        }
        else{
            	basicFeaturePanel.setVisible(false);
            	optionalFeaturePanel.setVisible(false);
            	parent.getHtsubscripedplanoptions().remove(parent.getTab().getSelectedIndex());
        	}
	}
	
	//this method will update the subscribed plan list
	private void performchkboxChange(ActionEvent e){
		isParentVORequired = true;
		JCheckBox checkbox = (JCheckBox)e.getSource();
		System.out.println(checkbox.getText());
		System.out.println(checkbox.isSelected());
		if(checkbox.isSelected()){
			Iterator<FeatureVO> iterator = mobilevoiceVO.getAllOptionalFeature().iterator();
			while(iterator.hasNext()){
				FeatureVO optfeature = iterator.next();
				if(optfeature.getName().equals(checkbox.getText())){
					subscribedoptionalfeatures.add(optfeature);
					break;
				}
			}
			System.out.println(subscribedoptionalfeatures.size());
		}
		else{
			Iterator<FeatureVO> iterator = subscribedoptionalfeatures.iterator();
			while(iterator.hasNext()){
				FeatureVO optfeature = iterator.next();
				if(optfeature.getName().equals(checkbox.getText())){
					subscribedoptionalfeatures.remove(optfeature);
					break;
				}
			}
		}
			subscribedmobilevoiceVO.addOptionalFeatures(subscribedoptionalfeatures);
    		parent.setSubscripedplanoptions(parent.getTab().getSelectedIndex(),subscribedmobilevoiceVO);
			//parent.getSubscripedplanoptions().add(subscribedmobilevoiceVO);
			//parent.setSubscripedplanoptions(subscribedmobilevoiceVO);
			System.out.println("subscribedmobilevoiceVO   "+subscribedoptionalfeatures.size());
		
		
	}
	
	private JPanel createSelectionPanel( ){
		JPanel p = new JPanel ();
		p.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)),""));
		
		chkexclude=new JCheckBox("Exculde Mobile Voice ");
		p.add(chkexclude);
		p.setPreferredSize(new Dimension(350, 60));
		
		chkexclude.addActionListener (new ActionListener () {
	            public void actionPerformed (ActionEvent e) {
	            	performchkexcludeChange();
	            		
	            }
	        });
		 
		 GroupLayout layout = new GroupLayout(p);
	        p.setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGap(15, 15, 15)
	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                    .addComponent(chkexclude, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
	                .addContainerGap(20, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGap(15, 15, 15)
	                .addComponent(chkexclude, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(20, Short.MAX_VALUE))
	        );
		return p;
	}
	
	private JPanel createBasicFeaturePanel(){
		JPanel p = new JPanel ();
		p.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)),"Basic Feature"));
		List<JCheckBox> checkboxlist = new ArrayList<JCheckBox>();
		JCheckBox checkbox;
		checkbox= new JCheckBox(mobilevoiceVO.getLocalCallFeatureVO().getName(),true);
		checkbox.setEnabled(false);
		checkboxlist.add(checkbox);

		 GroupLayout layout = new GroupLayout(p);
	        p.setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGap(24, 24, 24)
	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                    .addComponent(checkboxlist.get(0), GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
	                .addContainerGap(108, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGap(29, 29, 29)
	                .addComponent(checkboxlist.get(0), GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(214, Short.MAX_VALUE))
	        );
	    p.setPreferredSize(new Dimension(350, 100));
		return p;
	}
	
	private JPanel createOptionalFeaturePanel(){
		JPanel p = new JPanel ();
		p.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)),"Optional Features"));

		List<JCheckBox> checkboxlist = new ArrayList<JCheckBox>();
		JCheckBox checkbox;
		
		Iterator<FeatureVO> iterator = mobilevoiceVO.getAllOptionalFeature().iterator();
		while(iterator.hasNext()){
			FeatureVO feature;
			feature= iterator.next();
			checkbox= new JCheckBox(feature.getName(), false);
			checkbox.setEnabled(true);
			checkboxlist.add(checkbox);
			Iterator<FeatureVO> iterator2 = subscribedmobilevoiceVO.getAllOptionalFeature().iterator();
			while(iterator2.hasNext()){
				FeatureVO feature2;
				feature2= iterator2.next();
				if(feature2.getName().equalsIgnoreCase(feature.getName()))
					checkbox.setSelected(true);
			}
			checkbox.addActionListener (new ActionListener () {
	            public void actionPerformed (ActionEvent e) {
	            	performchkboxChange(e);
	            }
	        });
		}

		 GroupLayout layout = new GroupLayout(p);
	        p.setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	            		 .addGap(24, 24, 24)
	                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                        .addComponent(checkboxlist.get(0))
	                        .addComponent(checkboxlist.get(2)))
	                    .addGap(24, 24, 24)
	                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                    		.addComponent(checkboxlist.get(1)))
	                    		 .addContainerGap(214, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createSequentialGroup()//(GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                        .addComponent(checkboxlist.get(0))
	                        .addComponent(checkboxlist.get(1)))
	                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                        .addComponent(checkboxlist.get(2)))
	                        )

	        );
	    p.setPreferredSize(new Dimension(350, 220));
		return p;
	}
	
	private JPanel createButtonPanel () {

        JPanel p = new JPanel ();

        JButton previous = new JButton ("Previous");
        previous.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent e) {
            	//parent.getSubscripedplanoptions().remove(parent.getTab().getSelectedIndex());
            	if(!chkexclude.isSelected()){
 //                       	subscribedmobilevoiceVO.set(subscribedoptionalfeatures);
  //                      	subscribedmobilevoiceVO.setBaseFeature(mobilevoiceVO.getBaseFeature());
                        	//parent.getSubscripedplanoptions().put(Integer.toString(parent.getTab().getSelectedIndex()), subscribedmobilevoiceVO);
            	}
 
//            	if(isParentVORequired){
 //           			if(parent.getSubscripedplanoptions().contains(subscribedmobilevoiceVO))
//            						parent.getSubscripedplanoptions().remove(subscribedmobilevoiceVO);
            			

 //           		isParentVORequired = false;
   //         	}
               	parent.getTab().setSelectedIndex(parent.getTab().getSelectedIndex()-1);
            }
        });
        p.add (previous);
        
        JButton nextbutton = new JButton ("Next");
        nextbutton.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent e) {
            	//parent.getSubscripedplanoptions().remove(parent.getTab().getSelectedIndex());
            	if(!chkexclude.isSelected()){
  //                      	subscribedmobilevoiceVO.(subscribedoptionalfeatures);
 //                       	subscribedmobilevoiceVO.setBaseFeature(mobilevoiceVO.getBaseFeature());
                        	//parent.getSubscripedplanoptions().put(Integer.toString(parent.getTab().getSelectedIndex()), subscribedmobilevoiceVO);
            	}
            	
     //       	System.out.println(" Next subscribedmobilevoiceVO   Setting SubscriptionVO to Parent "+isParentVORequired +" " +subscribedmobilevoiceVO.getAccNo());
            	

            	parent.getTab().setSelectedIndex(parent.getTab().getSelectedIndex()+1);

            }
        });
        p.add (nextbutton);
        
        JButton close = new JButton ("Close");
        close.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent e) {
            	try {
					parent.setClosed(true);
				} catch (PropertyVetoException e1) {

				}
            }
        });
        p.add (close);

        JPanel bp = new JPanel ();
        bp.setLayout (new FlowLayout());
        bp.add (p);
        return bp;
    }

}
